package com.article.service.impl;

import com.article.dao.ArticleInfoDao;
import com.article.dto.ReturnError;
import com.article.enums.ReturnInfoEnum;
import com.article.lucene.ArticleIndex;
import com.article.model.ArticleInfo;
import com.article.service.ArticleInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2017/5/2.
 */
@Service("articleInfoService")
public class ArticleInfoServiceImpl implements ArticleInfoService {
    @Autowired
    private ArticleInfoDao articleInfoDao;

    private ArticleIndex articleIndex = new ArticleIndex();

    public Long countTotal(Map<String, Object> map) {
        return articleInfoDao.countTotal(map);
    }

    public List<ArticleInfo> listByPage(Map<String, Object> map) {
        return articleInfoDao.listByPage(map);
    }

    public List<ArticleInfo> listByMap(Map<String, Object> map) {
        return articleInfoDao.listByMap(map);
    }

    public ArticleInfo getById(Long id) {
        return articleInfoDao.getById(id);
    }

    public ArticleInfo getByMap(Map<String, Object> map) {
        return articleInfoDao.getByMap(map);
    }

    @Transactional
    public ReturnError saveArticle(ArticleInfo articleInfo) {
        try {
            int result = articleInfoDao.save(articleInfo);
            if (result != 0) {
                articleIndex.addIndex(articleInfo);
                return new ReturnError(false, ReturnInfoEnum.SUCCESS.getType(), ReturnInfoEnum.SUCCESS.getInfo(), null);
            }
            return new ReturnError(true, ReturnInfoEnum.FAIL.getType(), "操作失败", null);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Transactional
    public ReturnError updateArticle(ArticleInfo articleInfo) {
        try {
            int result = articleInfoDao.update(articleInfo);
            if (result != 0) {
                articleIndex.updateIndex(articleInfo);
                return new ReturnError(false, ReturnInfoEnum.SUCCESS.getType(), ReturnInfoEnum.SUCCESS.getInfo(), null);
            }
            return new ReturnError(true, ReturnInfoEnum.FAIL.getType(), "操作失败", null);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    @Transactional
    public ReturnError update(ArticleInfo articleInfo) {
        try {
            int result = articleInfoDao.update(articleInfo);
            return new ReturnError(false, ReturnInfoEnum.SUCCESS.getType(), ReturnInfoEnum.SUCCESS.getInfo(), null);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Transactional
    public ReturnError removeArticle(String ids) {
        try {
            String[] idarr = ids.split(",");
            for (String id : idarr) {
                int i = articleInfoDao.remove(Long.parseLong(id));
                if (i != 0) {
                    articleIndex.deleteIndex("" + id);
                }
            }
            return new ReturnError(false, ReturnInfoEnum.SUCCESS.getType(), null);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public List<ArticleInfo> listCount() {
        return articleInfoDao.listCount();
    }

    public ArticleInfo getLastArticle(Long id) {
        return articleInfoDao.getLastArticle(id);
    }

    public ArticleInfo getNextArticle(Long id) {
        return articleInfoDao.getNextArticle(id);
    }
}
